//
//  PhotosViewController.m
//  RainbowFM
//
//  Created by Kratos on 14/12/18.
//  Copyright (c) 2014年 RB. All rights reserved.
//

#import "PhotosViewController.h"

#import "ZZSXImagesBrowsersView.h"

@interface PhotosViewController ()<ZZSXImagesBrowsersViewDelegate>
@property (nonatomic , strong) ZZSXImagesBrowsersView *imagesBrowsersView;//相册
@property (nonatomic , strong) UIView *headView;
@property (nonatomic , strong) UIView *buttumView;
@property (nonatomic , strong) UILabel *titleLabel;
@property (nonatomic , strong) UILabel *indexLabel;

@property (nonatomic , strong) NSMutableArray *images;
@end

@implementation PhotosViewController

-(instancetype)init
{
    self = [super init];
    if (self) {
        _images = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeContentViews];
    [self.imagesBrowsersView setImageUrls:_images placeholderImage:@"default_image_big"];
    if ([_images count] > 0) {
        [self.indexLabel setText:_F(@"1/%d",_images.count)];
    }else{
        [self.indexLabel setText:@"0/0"];
    }
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}

-(void)setImageUrls:(NSArray *)images
{
    [_images removeAllObjects];
    [_images addObjectsFromArray:images];
    
}

-(void)initializeContentViews
{
    //背景相册
    self.imagesBrowsersView = [[ZZSXImagesBrowsersView alloc]initWithFrame:self.view.bounds];
    self.imagesBrowsersView.delegate = self;
    [self.view addSubview:self.imagesBrowsersView];
    
    
    //下半部
    self.buttumView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.view.bounds) - 44.f, CGRectGetWidth(self.view.bounds), 44.f)];
    [self.buttumView setBackgroundColor:[UIColor clearColor]];
    
    self.indexLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, (CGRectGetHeight(self.buttumView.bounds) - 20.f)/2, CGRectGetWidth(self.view.bounds), 20.f)];
    [self.indexLabel setFont:[UIFont systemFontOfSize:16]];
    [self.indexLabel setTextColor:[UIColor whiteColor]];
    [self.indexLabel setTextAlignment:NSTextAlignmentCenter];
    [self.buttumView addSubview:self.indexLabel];
    
    [self.view addSubview:self.buttumView];
    
    //上半部
    self.headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetHeight(self.view.bounds), 64.f)];
    [self.headView setBackgroundColor:[UIColor redColor]];
    
//    UIImageView *headViewBg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"black_bar_bg"]];
//    [headViewBg setFrame:self.headView.bounds];
//    [self.headView addSubview:headViewBg];
    
    self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(60.f, 64.f - 20.f - 10.f, CGRectGetHeight(self.view.bounds) - 60.f * 2, 20.f)];
    [self.titleLabel setFont:[UIFont systemFontOfSize:16]];
    [self.titleLabel setTextColor:[UIColor whiteColor]];
    [self.titleLabel setTextAlignment:NSTextAlignmentCenter];
    [self.headView addSubview:self.titleLabel];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:CGRectMake(20.f, 28.f, 30.f, 30.f)];
    [button setImage:[UIImage imageNamed:@"home_back_button"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(navBackAction) forControlEvents:UIControlEventTouchUpInside];
    [self.headView addSubview:button];
    
    [self.view addSubview:self.headView];
}

#pragma mark - ZZSXImagesBrowsersViewDelegate
- (void)browsersDidSelectItemAtIndex:(NSInteger)index
{
    __weak typeof(self) self_ = self;
    if (self.buttumView.frame.origin.y == CGRectGetHeight(self.view.bounds)) {
        [UIView animateWithDuration:0.5 animations:^{
            [self.headView setFrame:CGRectMake(0,0, CGRectGetWidth(self_.headView.bounds), CGRectGetHeight(self_.headView.bounds))];
            
            [self_.buttumView setFrame:CGRectMake(0, CGRectGetHeight(self.view.bounds) - 44.f, CGRectGetWidth(self.view.bounds), 44.f)];
        }];
    }
    else
    {
        [UIView animateWithDuration:0.5 animations:^{
            [self_.headView setFrame:CGRectMake(0, - CGRectGetHeight(self_.headView.bounds), CGRectGetWidth(self_.headView.bounds), CGRectGetHeight(self_.headView.bounds))];
            
            [self_.buttumView setFrame:CGRectMake(0, CGRectGetHeight(self.view.bounds), CGRectGetWidth(self.view.bounds), 44.f)];
        }];
    }
}

- (void)browsersCurrentItemIndexDidChange:(NSInteger)index
{
    [self.indexLabel setText:_F(@"%d/%d",index+ 1,_images.count)];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
